1 00:00:00,330 --> 00:00:02,830 In this lesson, I'm going to make a health pickup. 2 00:00:02,850 --> 00:00:08,170 In the last lesson, we had the simple pickup that if we touch it, it disappears. 3 00:00:08,190 --> 00:00:10,860 That's cool and all, but not very helpful for our game. 4 00:00:10,860 --> 00:00:13,050 We want something like a health pickup. 5 00:00:13,230 --> 00:00:18,450 And before I get started with my game playing, I'm going to go to the workspace because I need to show 6 00:00:18,450 --> 00:00:19,260 you something. 7 00:00:19,260 --> 00:00:20,730 I'm going to open a workspace. 8 00:00:20,730 --> 00:00:22,050 Here's my character. 9 00:00:22,050 --> 00:00:25,110 If I select it, you can see that it's highlighted in the world. 10 00:00:25,230 --> 00:00:28,680 If I open it, I'll look for something called the humanoid. 11 00:00:28,710 --> 00:00:31,590 The humanoid allows me to run around and jump. 12 00:00:31,590 --> 00:00:33,330 It's a special Roblox object. 13 00:00:33,330 --> 00:00:34,920 It also controls health. 14 00:00:34,920 --> 00:00:40,440 So if humanoid selected, let's go down and look for something that says health. 15 00:00:41,100 --> 00:00:41,970 Here's health. 16 00:00:41,970 --> 00:00:45,390 We have health, which is 100 and max health. 17 00:00:45,390 --> 00:00:46,740 Remember those two? 18 00:00:46,740 --> 00:00:52,710 So if you have health of ten changes of ten, you're going to watch. 19 00:00:52,710 --> 00:00:55,590 It's going to show some sort of damage here. 20 00:00:56,010 --> 00:01:00,000 Bam, not much, but it's down to ten points max. 21 00:01:00,000 --> 00:01:02,550 Health is 100, so we're at 10%. 22 00:01:02,550 --> 00:01:04,200 What happens if I go to zero? 23 00:01:05,040 --> 00:01:07,380 I died anyway. 24 00:01:07,380 --> 00:01:08,700 So this is what we're going to work with. 25 00:01:08,700 --> 00:01:10,830 We're going to work with the health and the max health. 26 00:01:11,010 --> 00:01:15,300 Let's go ahead and look at our guy, our little player again. 27 00:01:15,510 --> 00:01:16,710 Go to humanoid. 28 00:01:16,710 --> 00:01:22,740 Notice that the humanoid is one level down from the character, right? 29 00:01:22,740 --> 00:01:25,140 That is called a first child. 30 00:01:25,410 --> 00:01:28,380 The next level is not the grandchild. 31 00:01:28,380 --> 00:01:28,650 Right? 32 00:01:28,650 --> 00:01:29,970 That's the second level. 33 00:01:29,970 --> 00:01:31,080 The second child. 34 00:01:31,080 --> 00:01:33,540 So remember that term, first child. 35 00:01:33,540 --> 00:01:35,220 All these are first children. 36 00:01:35,220 --> 00:01:37,230 They're right underneath the character. 37 00:01:37,230 --> 00:01:39,660 You got to know that for what we're going to do today. 38 00:01:39,660 --> 00:01:41,940 So let's go ahead and turn this off. 39 00:01:42,920 --> 00:01:46,760 Go to our workspace, pick up. 40 00:01:46,760 --> 00:01:47,960 There's the pickup. 41 00:01:48,080 --> 00:01:49,940 I'm going to change this to health. 42 00:01:49,940 --> 00:01:50,720 Pick up. 43 00:01:54,190 --> 00:01:55,120 Here we go. 44 00:01:56,110 --> 00:01:58,420 And I'll change the script name. 45 00:01:58,450 --> 00:01:58,690 Oops. 46 00:01:58,690 --> 00:02:00,170 I selected it, but that's okay. 47 00:02:00,190 --> 00:02:05,890 We're going to use it and I'll change that to let's just call it give health. 48 00:02:09,620 --> 00:02:10,440 Here we go. 49 00:02:10,460 --> 00:02:15,200 Now, we already got a variable for the pickup and we have this touch function. 50 00:02:15,230 --> 00:02:20,150 The other part is what's going to be touching my pickup, which is going to be like a hand or a foot. 51 00:02:20,150 --> 00:02:23,790 But it could be other things too, like a ball or a tree or whatever. 52 00:02:23,810 --> 00:02:28,580 We're not really checking for that, but we need to have a print statement here to see what touched 53 00:02:28,580 --> 00:02:29,000 it. 54 00:02:29,510 --> 00:02:32,060 We saw that last video, so we'll delete that. 55 00:02:32,330 --> 00:02:40,070 And what I want to do is I want to get a part and check to see if it's associated with the humanoid 56 00:02:40,070 --> 00:02:43,400 that I know is like a character, some sort of character with a humanoid in it. 57 00:02:44,150 --> 00:02:50,540 I'm going to make a variable local cube and humans are going to be short for humanoid. 58 00:02:50,810 --> 00:02:53,270 I'm going to look for a humanoid. 59 00:02:53,390 --> 00:02:59,800 I'm going to get the other part, which might be a hand or a foot or a head with a dot parent. 60 00:02:59,810 --> 00:03:02,440 That takes me up to the character level. 61 00:03:02,450 --> 00:03:05,150 The hands parent is the character. 62 00:03:05,510 --> 00:03:13,170 I'm going to try and find first child that's a humanoid. 63 00:03:13,190 --> 00:03:16,640 Remember I said that that was the first child that was underneath the character. 64 00:03:16,820 --> 00:03:20,480 If we find the humanoid, this is going to have value. 65 00:03:20,480 --> 00:03:22,200 It's going to be the humanoid. 66 00:03:22,220 --> 00:03:25,490 If we don't find the value, this is going to be nil. 67 00:03:25,520 --> 00:03:26,450 Just be careful. 68 00:03:26,450 --> 00:03:30,000 If you spell this wrong, you won't find it, but you won't get an error. 69 00:03:30,020 --> 00:03:31,970 It's just not going to work and you're going to have trouble. 70 00:03:32,000 --> 00:03:32,870 That's a little bit hard. 71 00:03:33,050 --> 00:03:36,950 Hard to deal with, but you'll get better at this as it goes along. 72 00:03:37,550 --> 00:03:47,690 Let's say if human does not equal nil, then we found a humanoid. 73 00:03:47,720 --> 00:03:49,010 We'll put the end here. 74 00:03:49,820 --> 00:03:51,350 So we've got a humanoid. 75 00:03:51,740 --> 00:03:53,600 We have access to the health. 76 00:03:55,340 --> 00:03:59,180 HUME Dot health. 77 00:03:59,720 --> 00:04:02,400 Let's make it equal to full health. 78 00:04:02,420 --> 00:04:03,740 Remember what that was? 79 00:04:03,950 --> 00:04:05,290 It was 100. 80 00:04:05,300 --> 00:04:07,790 But this is called a literal, right? 81 00:04:07,790 --> 00:04:10,730 You're putting the actual number in your script. 82 00:04:10,760 --> 00:04:13,420 Not really that great of a practice. 83 00:04:13,430 --> 00:04:15,690 We could do something like this. 84 00:04:15,710 --> 00:04:19,100 HUME Dot max health. 85 00:04:19,970 --> 00:04:24,620 And that's going to give us full health if the humanoid exists or whoever touches it. 86 00:04:24,620 --> 00:04:28,340 And they have a humanoid, so a zombie could touch it and get that, too. 87 00:04:28,430 --> 00:04:31,820 What happens if we don't find the humanoid? 88 00:04:31,820 --> 00:04:33,920 Well, it will be nil. 89 00:04:33,950 --> 00:04:35,330 This will be false. 90 00:04:35,330 --> 00:04:36,650 So the humanoid. 91 00:04:37,360 --> 00:04:42,460 Not equally nil will be a false statement, and then we'll just skip it. 92 00:04:42,790 --> 00:04:44,110 Let's see what we got. 93 00:04:44,380 --> 00:04:48,000 Let's put let's put a zombie in there to hurt us, though. 94 00:04:48,010 --> 00:04:50,110 We want to check to see if we get full health. 95 00:04:50,470 --> 00:04:53,140 Let's say drooling zombie. 96 00:04:53,980 --> 00:04:57,010 This is a Roblox product, so theoretically. 97 00:04:57,010 --> 00:04:57,790 Good. 98 00:04:59,780 --> 00:05:00,800 Cool. 99 00:05:01,220 --> 00:05:01,640 Yeah. 100 00:05:01,640 --> 00:05:03,830 And it warns you that there are scripts. 101 00:05:03,830 --> 00:05:05,510 I trust these scripts, though. 102 00:05:06,340 --> 00:05:10,330 But you should be careful because people can put bad stuff in the scripts. 103 00:05:10,570 --> 00:05:11,830 Let's hit the play. 104 00:05:12,800 --> 00:05:13,040 Now. 105 00:05:13,040 --> 00:05:13,950 We don't want to hit. 106 00:05:13,970 --> 00:05:18,410 We don't want to hit that health right away or we're going to use it up and we're not going to get any 107 00:05:18,410 --> 00:05:19,370 benefits from it. 108 00:05:19,370 --> 00:05:20,600 So we want to get hurt. 109 00:05:20,960 --> 00:05:22,090 We got a little bit of hurt. 110 00:05:22,100 --> 00:05:23,890 We're down we're down to the yellow. 111 00:05:23,900 --> 00:05:26,120 We're going to touch this boom. 112 00:05:26,120 --> 00:05:27,280 It's gone. 113 00:05:27,290 --> 00:05:33,500 So the health bar, when you're 100% healthy, disappears, that's pretty cool. 114 00:05:33,530 --> 00:05:39,260 Let's go ahead and make sure that we can't touch it when we're full of health, though. 115 00:05:40,430 --> 00:05:41,860 So go to our health strip. 116 00:05:42,560 --> 00:05:43,640 How do we do that? 117 00:05:43,640 --> 00:05:46,400 Well, check to see if the humanoid exists. 118 00:05:47,140 --> 00:05:51,130 And if it does, then we'll say if. 119 00:05:51,160 --> 00:05:54,910 HUME Dot health is less than. 120 00:05:55,330 --> 00:05:58,120 HUME Dot Mac's health. 121 00:05:59,870 --> 00:06:04,460 Then and I'm going to format this. 122 00:06:05,780 --> 00:06:07,310 So now we have two checks. 123 00:06:07,310 --> 00:06:09,230 We check to see if the humanoid exists. 124 00:06:09,230 --> 00:06:09,620 Right. 125 00:06:09,620 --> 00:06:11,330 Make sure it's not nil. 126 00:06:11,630 --> 00:06:17,270 If it does exist, we want to make sure that the health is, in fact, less than max health. 127 00:06:17,300 --> 00:06:20,510 Then we will award health, destroy the script. 128 00:06:20,510 --> 00:06:24,260 Anything that goes wrong here, we're going to skip right out of that function. 129 00:06:24,260 --> 00:06:25,820 Nothing's going to happen. 130 00:06:26,060 --> 00:06:27,200 So go ahead and play it. 131 00:06:27,940 --> 00:06:32,020 We should be able to run through this thing and not destroy it. 132 00:06:32,020 --> 00:06:34,540 Yeah, because we're full of health, right? 133 00:06:34,540 --> 00:06:35,650 Then when we get hurt. 134 00:06:37,960 --> 00:06:39,250 Cool beans. 135 00:06:39,340 --> 00:06:41,260 We are 100% health. 136 00:06:41,740 --> 00:06:42,100 All right. 137 00:06:42,100 --> 00:06:45,550 One more thing I want to go over in our give health. 138 00:06:45,550 --> 00:06:50,710 So we have this embedded if statement, it's inside another statement. 139 00:06:50,770 --> 00:06:56,230 Well, we know a little bit about if statements and we have these logical operators where we can chain 140 00:06:56,230 --> 00:06:57,190 conditions together. 141 00:06:57,190 --> 00:07:01,690 So we don't need to if statements, but it's perfectly fine to have to. 142 00:07:02,020 --> 00:07:07,060 We could do this and and then get this. 143 00:07:09,350 --> 00:07:13,130 Put it here and then let's get rid of this. 144 00:07:13,130 --> 00:07:17,330 If statement, don't forget the end, then we'll format it. 145 00:07:17,960 --> 00:07:19,070 There we go. 146 00:07:19,850 --> 00:07:22,550 Get rid of those blank spaces. 147 00:07:23,610 --> 00:07:24,580 A blank lines. 148 00:07:24,600 --> 00:07:27,670 Now, what happens if this is nil? 149 00:07:27,690 --> 00:07:28,380 Right. 150 00:07:28,590 --> 00:07:30,710 Well, this we have a problem, right? 151 00:07:30,720 --> 00:07:33,960 Because we're trying to access health on a nil object. 152 00:07:33,960 --> 00:07:36,600 Well, obviously doesn't have health if it's nil. 153 00:07:36,840 --> 00:07:40,770 Well, we don't have to worry, because what's going to happen is we're going to check this. 154 00:07:40,920 --> 00:07:47,970 If it is not nil, then we're going to go and we're going to check this whole condition. 155 00:07:48,540 --> 00:07:49,020 Right. 156 00:07:49,020 --> 00:07:57,090 And it will chunk these the logical operator is lower priority than this comparison right here. 157 00:07:57,090 --> 00:08:03,720 But if you're worried about that, you can always use parentheses to make sure that you're grouping 158 00:08:03,720 --> 00:08:05,520 your grouping your statements. 159 00:08:05,520 --> 00:08:07,500 Well, cool. 160 00:08:07,500 --> 00:08:13,320 So another thing that we can do, roadblocks is tricky like this. 161 00:08:13,320 --> 00:08:20,520 This is supposed to be true or false, but if something exists and there's no comparison, it will be 162 00:08:20,520 --> 00:08:21,720 considered true. 163 00:08:21,990 --> 00:08:25,230 If it does not exist, it's nil. 164 00:08:25,230 --> 00:08:26,490 In other words, it's nil. 165 00:08:26,490 --> 00:08:28,020 It will be considered false. 166 00:08:28,020 --> 00:08:35,520 So we can actually say, Hey, if HUME and that's checking to see if it exists, then we can do this 167 00:08:35,520 --> 00:08:36,450 comparison. 168 00:08:36,870 --> 00:08:37,830 Pretty cool. 169 00:08:40,110 --> 00:08:41,640 Let's see if it works. 170 00:08:42,720 --> 00:08:42,870 All right. 171 00:08:42,960 --> 00:08:45,450 We can run through our little ball there. 172 00:08:45,480 --> 00:08:47,400 Let's check our output window. 173 00:08:48,390 --> 00:08:49,620 That's looking good. 174 00:08:50,280 --> 00:08:51,430 Make this a little bigger. 175 00:08:51,660 --> 00:08:52,410 Get hurt. 176 00:08:53,160 --> 00:08:54,660 Bomber or yellow? 177 00:08:57,120 --> 00:08:58,260 Cool beans. 178 00:08:58,290 --> 00:09:00,420 We are full of health. 179 00:09:01,140 --> 00:09:06,960 So that is a simple health pickup and we will just keep building on this until you get your own gain.